package com.example.liwushuo;

import com.alibaba.fastjson.JSONObject;
import com.example.liwushuo.bar.LittleGoodsBar;
import com.example.liwushuo.bean.homegridview.GridItem;
import com.example.liwushuo.bean.homegridview.glzt.BaseAllZT;
import com.example.liwushuo.fragment.Fragment_AllZT;
import com.example.liwushuo.urls.urls;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Window;
import android.widget.Toast;

public class LittleGoodsItem extends ActionBarActivity {

	private int id;
	private String url;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_little_goods_item);

		id = getIntent().getIntExtra("id", -1);
		if (id != -1 && id == 1) {
			url = urls.getLittleGoodsUrl;
		} else if (id != -1 && id == 2) {
			url = urls.getGLZTUrl;
		}

		ititData();
	}

	private void ititData() {

		BaseApp.getHttpUtils().send(HttpMethod.GET, url,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {

						Toast.makeText(getApplicationContext(), "网络连接失败，请检查网络",
								1).show();

					}

					@Override
					public void onSuccess(ResponseInfo<String> response) {

						String json = response.result;
						Log.i("little", "--------" + json);

						if (id != -1 && id == 1) {

							GridItem gridItem = JSONObject.parseObject(json,
									GridItem.class);

							if (gridItem != null
									&& "OK".equals(gridItem.getMessage())) {
								LittleGoodsBar littleGoodsBar = new LittleGoodsBar();
								Bundle args = new Bundle();
								args.putSerializable("gridItem", gridItem);
								littleGoodsBar.setArguments(args);
								getSupportFragmentManager()
										.beginTransaction()
										.replace(R.id.little_goods,
												littleGoodsBar).commit();
							}
						} else if (id != -1 && id == 2) {
							BaseAllZT baseAllZT = JSONObject.parseObject(json,
									BaseAllZT.class);
							if (baseAllZT != null
									&& "OK".equals(baseAllZT.getMessage())) {

								Fragment_AllZT fragment_AllZT = new Fragment_AllZT();
								Bundle args = new Bundle();
								args.putSerializable("baseAllZT", baseAllZT);
								fragment_AllZT.setArguments(args);
								getSupportFragmentManager()
										.beginTransaction()
										.replace(R.id.little_goods,
												fragment_AllZT).commit();

							}

						}
					}
				});
	}
}
